//    Copyright (C) Kherty.  All rights reserved.
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;

namespace OpenLS.Spreadsheet.UI
{
    public class WorksheetSplitter: Thumb //\\ move to style?
    {
        static WorksheetSplitter()
        {
            EventManager.RegisterClassHandler(typeof(WorksheetSplitter), DragStartedEvent, new DragStartedEventHandler(OnDragStarted));
            EventManager.RegisterClassHandler(typeof(WorksheetSplitter), DragCompletedEvent, new DragCompletedEventHandler (OnDragCompleted));
            EventManager.RegisterClassHandler(typeof(WorksheetSplitter), DragDeltaEvent, new DragDeltaEventHandler (OnDragDelta));

        }
        static void OnDragDelta(object sender, DragDeltaEventArgs args)
        {
            Debug.WriteLine("on drag delta " + args.HorizontalChange + " " + args.VerticalChange);
            WorksheetSplitter This = (WorksheetSplitter)sender;
            Worksheet ws = SpreadsheetElement.GetWorksheet(This);
            double w = ws.View.Pane.Width.GetValueOrDefault(0);
            double h = ws.View.Pane.Height.GetValueOrDefault(0);
            Point pt = new Point(w + args.HorizontalChange, h + args.VerticalChange);
            CellAddress a = ws.TopLeftPane.GetCellClosestAddressAt(pt);
            if (This.verticalPreview != null && ws.View.Pane.Width != null)
                This.verticalPreview.OffsetX = - w + (ws.Columns[a.Column].Offset - ws.Columns[ws.TopLeftPane.Left].Offset); //\\ GetClosestX(sender, args.HorizontalChange);
            if (This.horizontalPreview != null && ws.View.Pane.Height != null)
                This.horizontalPreview.OffsetY = - h + (ws.Rows[a.Row].Offset - ws.Rows[ws.TopLeftPane.Top].Offset);
                //\\This.horizontalPreview.OffsetY = GetClosestY (sender, args.VerticalChange);
        }

        static void OnDragCompleted(object sender, DragCompletedEventArgs args)
        {
            WorksheetSplitter This = (WorksheetSplitter)sender;
            WorksheetPanel grid = This.Parent as WorksheetPanel;
            Worksheet ws = SpreadsheetElement.GetWorksheet(grid);
            if (This.ResizeDirection != WorksheetResizeDirection.Rows)
                ws.View.Pane.Width = ws.View.Pane.Width.Value + args.HorizontalChange;
            if (This.ResizeDirection != WorksheetResizeDirection.Columns)
                ws.View.Pane.Height = ws.View.Pane.Height.Value + args.VerticalChange;
            foreach (WorksheetPane pane in ws.GetPanes())
            {
                pane.NotifyPropertyChanged("DesiredWidth");
                pane.NotifyPropertyChanged("DesiredHeight");
            }
            AdornerLayer layer = AdornerLayer.GetAdornerLayer(grid);
            if (This.verticalPreview != null)
                layer.Remove(This.verticalPreview);
            if (This.horizontalPreview != null)
                layer.Remove(This.horizontalPreview);
        }
        static void OnDragStarted(object sender, DragStartedEventArgs args)
        {
            WorksheetSplitter This = (WorksheetSplitter)sender;
            WorksheetPanel grid = This.Parent as WorksheetPanel;
            AdornerLayer layer = AdornerLayer.GetAdornerLayer(grid);
            Style style = null;
            style = This.PreviewStyle;
            if (This.ResizeDirection != WorksheetResizeDirection.Rows)
            {
                WorksheetSplitter rowsSplitter = This.RowsSplitter;
                if (rowsSplitter == null)
                    rowsSplitter = This;
                This.verticalPreview = new PreviewAdorner(rowsSplitter, style);
                layer.Add(This.verticalPreview);
            }
            if (This.ResizeDirection != WorksheetResizeDirection.Columns)
            {
                WorksheetSplitter columnsSplitter = This.ColumnsSplitter;
                if (columnsSplitter == null)
                    columnsSplitter = This;
                This.horizontalPreview = new PreviewAdorner(columnsSplitter, style);
                layer.Add(This.horizontalPreview);
            }
        }
        PreviewAdorner verticalPreview, horizontalPreview;


        public Style PreviewStyle
        {
            get { return (Style)GetValue(PreviewStyleProperty); }
            set { SetValue(PreviewStyleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for PreviewStyle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PreviewStyleProperty =
            DependencyProperty.Register("PreviewStyle", typeof(Style), typeof(WorksheetSplitter), new UIPropertyMetadata(null));




        public WorksheetSplitter RowsSplitter
        {
            get { return (WorksheetSplitter)GetValue(RowsSplitterProperty); }
            set { SetValue(RowsSplitterProperty, value); }
        }

        // Using a DependencyProperty as the backing store for RowsSplitter.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RowsSplitterProperty =
            DependencyProperty.Register("RowsSplitter", typeof(WorksheetSplitter), typeof(WorksheetSplitter), new UIPropertyMetadata(null));




        public WorksheetSplitter ColumnsSplitter
        {
            get { return (WorksheetSplitter)GetValue(ColumnsSplitterProperty); }
            set { SetValue(ColumnsSplitterProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ColumnsSplitter.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ColumnsSplitterProperty =
            DependencyProperty.Register("ColumnsSplitter", typeof(WorksheetSplitter), typeof(WorksheetSplitter), new UIPropertyMetadata(null));

	

        public WorksheetResizeDirection ResizeDirection
        {
            get { return (WorksheetResizeDirection)GetValue(OrientationProperty); }
            set { SetValue(OrientationProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Orientation.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OrientationProperty =
            DependencyProperty.Register("ResizeDirection", typeof(WorksheetResizeDirection), typeof(WorksheetSplitter), new UIPropertyMetadata(WorksheetResizeDirection.Columns));





    }
}
